#include "yhsocket.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>


int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("usage: %s <domain service path>\n", argv[0]);
        return 1;
    }
    int fd = unix_domain_connect_udp(argv[1]);
    if (fd < 0) {
        printf("unix_domain_connect_udp error %d: %s\n", errno, strerror(errno));
        return 1;
    }

    int n, ret;
    char buff[1024] = {0};

    while (1) {
        printf("keyboard input: ");
        fflush(stdout);
        ret = read(0, buff, sizeof(buff));
        if (ret < 0) {
            printf("input error %d: %s\n", errno, strerror(errno));
            return 1;
        } else if (ret == 0) {
            printf("quit.\n");
            break;
        }
        n = strlen(buff);
        if (buff[n-1] == '\n')
            buff[n-1]  = '\0';
        ret = send(fd, buff, n, 0);
        if (ret > 0) {
            printf("send %d \"%s\"\n", ret, buff);
        } else {
            printf("send %d error %d: %s\n", ret, errno, strerror(errno));
        }
        memset(buff, 0, sizeof(buff));
    }

    return 0;
}
